﻿using UnityEngine;
using UnityEngine.UI;
using GameFramework.Localization;
using UnityGameFramework.Runtime;

namespace MYSTARFORCE
{
    public class SettingForm : UGuiForm
    {

        // 音乐的开关
        [SerializeField]
        private Toggle m_MusicMuteToggle = null;

        // 音乐滑动条控制音量大小
        [SerializeField]
        private Slider m_MusicVolumeSlider = null;

        // 非界面音效的开关
        [SerializeField]
        private Toggle m_SoundMuteToggle = null;

        [SerializeField]
        private Slider m_SoundVolumeSlider = null;

        // 界面音效的开关
        [SerializeField]
        private Toggle m_UISoundMuteToggle = null;

        [SerializeField]
        private Slider m_UISoundVolumeSlider = null;

        // 重启提示主节点
        [SerializeField]
        private CanvasGroup m_LanguageTipsCanvasGroup = null;

        // 选择英语
        [SerializeField]
        private Toggle m_EnglishToggle = null;

        // 选择简体中文
        [SerializeField]
        private Toggle m_ChineseSimplifiedToggle = null;

        // 选择繁体中文
        [SerializeField]
        private Toggle m_ChineseTraditionalToggle = null;

        // 默认选择语言
        private Language m_SelectedLanguage = Language.Unspecified;

        public void OnMusicMuteChanged(bool isOn)
        {
            GameEntry.Sound.Mute("Music", !isOn);
            m_MusicVolumeSlider.gameObject.SetActive(isOn);
        }

        public void OnMusicVolumeChanged(float volume)
        {
            GameEntry.Sound.SetVolume("Music", volume);
        }

        public void OnSoundMuteChanged(bool isOn)
        {
            GameEntry.Sound.Mute("Sound", !isOn);
            m_SoundVolumeSlider.gameObject.SetActive(isOn);
        }

        public void OnSoundVolumeChanged(float volume)
        {
            GameEntry.Sound.SetVolume("Sound", volume);
        }

        public void OnUISoundMuteChanged(bool isOn)
        {
            GameEntry.Sound.Mute("UISound", !isOn);
            m_UISoundVolumeSlider.gameObject.SetActive(isOn);
        }


        public void OnUISoundVolumeChanged(float volume)
        {
            GameEntry.Sound.SetVolume("UISound", volume);
        }

        // 选择英文
        public void OnEnglishSelected(bool isOn)
        {
            if (!isOn)
            {
                return;
            }
            m_SelectedLanguage = Language.English;
            RefreshLanguageTips();
        }

        // 选择简体中文
        public void OnChineseSimplifiedSelected(bool isOn)
        {
            if (!isOn)
            {
                return;
            }
            m_SelectedLanguage = Language.ChineseSimplified;
            RefreshLanguageTips();
        }

        // 选择繁体中文
        public void OnChineseTraditionalSelected(bool isOn)
        {
            if (!isOn)
            {
                return;
            }
            m_SelectedLanguage = Language.ChineseTraditional;
            RefreshLanguageTips();
        }

        /// <summary>
        /// 点击确认按钮
        /// </summary>
        public void OnConfirmButtonClick()
        {
            // 如果选择的语言和当前游戏语言相同，就关闭界面
            if(m_SelectedLanguage == GameEntry.Localization.Language)
            {
                Close();
                return;
            }

            // 否则就将所选择的语言保存成游戏语言
            GameEntry.Setting.SetString(Constant.Setting.Language, m_SelectedLanguage.ToString());
            GameEntry.Setting.Save();
            // 重启之前需要停止播放音乐？？
            GameEntry.Sound.StopMusic();

            // 然后重启游戏
            UnityGameFramework.Runtime.GameEntry.Shutdown(ShutdownType.Restart);
        }

        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);

            // 根据当前音乐音效的开关，选择是否显示音乐音效的勾勾
            m_MusicMuteToggle.isOn = !GameEntry.Sound.IsMuted("Music");
            m_SoundMuteToggle.isOn = !GameEntry.Sound.IsMuted("Sound");
            m_UISoundMuteToggle.isOn = !GameEntry.Sound.IsMuted("UISound");

            // 根据当前音乐音效的音量，设定滚动条的取值比例
            m_MusicVolumeSlider.value = GameEntry.Sound.GetVolume("Music");
            m_SoundVolumeSlider.value = GameEntry.Sound.GetVolume("Sound");
            m_UISoundVolumeSlider.value = GameEntry.Sound.GetVolume("UISound");

            m_SelectedLanguage = GameEntry.Localization.Language;
            //根据当前的语言选择开启哪个语言的勾勾
            switch (m_SelectedLanguage)
            {
                case Language.English:
                    m_EnglishToggle.isOn = true;
                    break;
                case Language.ChineseSimplified:
                    m_ChineseSimplifiedToggle.isOn = true;
                    break;
                case Language.ChineseTraditional:
                    m_ChineseTraditionalToggle.isOn = true;
                    break;

                default:
                    break;
            }
        }


        protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(elapseSeconds, realElapseSeconds);

            // 当选择了和当前游戏不同的语言的时候，提示文本就会循环闪烁
            if (m_LanguageTipsCanvasGroup.gameObject.activeSelf)
            {
                m_LanguageTipsCanvasGroup.alpha = 0.5f + 0.5f * Mathf.Sin(Mathf.PI * Time.time);
            }
        }

        /// <summary>
        /// 刷新提示语
        /// </summary>
        private void RefreshLanguageTips()
        {
            m_LanguageTipsCanvasGroup.gameObject.SetActive(m_SelectedLanguage != GameEntry.Localization.Language);
        }


    }
}